package com.vinfai.patterns.observers;

import java.util.ArrayList;
import java.util.List;

public class WeatherData implements ISubject {
	
	public List<Observer> observerList = new ArrayList<Observer>();
	
	public double temperature ;
	public double pressure;
	
	@Override
	public void notifyObservers() {
		for(Observer o : observerList){
			o.update(this);
		}
	}

	@Override
	public void registerObservers(Observer observer) {
		if(!observerList.contains(observer)){
			observerList.add(observer);
		}
	}

	@Override
	public void removeObservers(Observer observer) {
		if(observerList.contains(observer)){
			observerList.remove(observer);
		}
	}
	
	
	public Double getTemperature(){
		this.temperature = Math.abs((Math.random()*100-50));
		return temperature;
	}
	
	public Double getPressure(){
		this.pressure = 102.3d;
		return pressure;
	}
	
	
	public void setMeasureData(double temp,double pressure){
		if(this.temperature!=temp||this.pressure!=pressure){
			this.notifyObservers();
		}
	}

}
